﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace eBrain
{
    public class RoiListElement
    {
        public int x0, y0, x1, y1;
        public int width, height;
        public int[] ROISelectedArray; //Array contenente la sequenza di 0 se non è selezionata, 1 se è selezionata
        public int RoiSelArrayCounter = 0;
        private int RoiSelArraySize = 0;
        private int ROISelArrayThreshold = 0;
        public string sItem = "";
        public string sID = "";
        public int iValue = 0; 
        public bool bActive = false; 
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_x0"></param>
        /// <param name="_y0"></param>
        /// <param name="_width"></param>
        /// <param name="_height"></param>
        /// <param name="_ID"></param>
        public RoiListElement(int _x0, int _y0, int _width, int _height, int _ROISelArraySize, int _ROISelArrayThreshold, bool _active, string _ID, string _item, int _value)
        {
            x0 = _x0;
            y0 = _y0;
            width = _width;
            height = _height;

            x1 = _x0 + width;
            y1 = _y0 + height;

            bActive = _active;
            
            sID = _ID;
            sItem = _item;
            iValue = _value;

            ROISelectedArray = new int[_ROISelArraySize];
            RoiSelArraySize = _ROISelArraySize;
            ROISelArrayThreshold = _ROISelArrayThreshold;
        }

        public bool CheckIfSustainedInROI()
        {
            int count = 0;

            for (int i = 0; i < RoiSelArraySize; i++)
                count += ROISelectedArray[i];
        
            if (count >= ROISelArrayThreshold)
                return true; 
            else
                return false; 
        }

        public void ClearROISelectedArray()
        {
            //TODO: verificare che per "cancellare" basti mettere i valori a 0 o se si debbano impostare a -1
            for (int i = 0; i < RoiSelArraySize; i++)
                ROISelectedArray[i] = -1;
        }
    }



    public class RoiList
    {
        public const int NO_ROI = -1;
        public const int MAIN_ROI = 0;
        public int ROI_to_highlight = NO_ROI;
        //public int ROI_Selected = NO_ROI;

        public List<RoiListElement> myRoiList;
        public int NumStepForSelection;
        public int ThrForSelection;

        public AreaOfInterestData GD_AreaCovered, GD_AreaSelected;

            public RoiList(int _NumStepForSelection, int _thrForSelectionPerc)
        {
            myRoiList = new List<RoiListElement>();
            NumStepForSelection = _NumStepForSelection;
            ThrForSelection = NumStepForSelection * _thrForSelectionPerc / 100;
            GD_AreaCovered = new AreaOfInterestData();
            GD_AreaSelected = new AreaOfInterestData();
                //ROI_Selected = NO_ROI;
        }

        public void UpdateParameters(int _NumStepForSelection, int _thrForSelectionPerc)
        {
            NumStepForSelection = _NumStepForSelection;
            ThrForSelection = NumStepForSelection * _thrForSelectionPerc / 100;
            //ROI_Selected = NO_ROI;
        }

        public void AddElement(int _x0, int y_0, int width, int height, bool _active, string _ID, string _item, int _value)
        {
            RoiListElement rle = new RoiListElement(_x0, y_0, width, height, NumStepForSelection, ThrForSelection, _active, _ID, _item, _value);

            myRoiList.Add(rle);
        }

        public void Clear()
        {
            ROI_to_highlight = NO_ROI;
            myRoiList.Clear();
            
        }

        public int IsSustainedInROI(int _x, int _y) 
        {
            int count = 0;
            //faccio il giro su tutte le ROIs

            ROI_to_highlight = NO_ROI;

            foreach (RoiListElement rle in myRoiList)
            {
                if (_x >= rle.x0 && _x <= rle.x1 && _y >= rle.y0 && _y <= rle.y1 && rle.bActive)
                {
                    ROI_to_highlight = count; 

                    //sono all'interno della ROI
                    //Segno 1 nell'array delle ultime ROI
                    GD_AreaCovered.x = rle.x0;
                    GD_AreaCovered.y = rle.y0;
                    GD_AreaCovered.w = rle.width;
                    GD_AreaCovered.h = rle.height;
                    GD_AreaCovered.item = rle.sItem;
                    GD_AreaCovered.value = rle.iValue;

                    rle.ROISelectedArray[rle.RoiSelArrayCounter] = 1;
                    if (rle.CheckIfSustainedInROI())
                    {
                        //ripulisco gli ultimi dati dell'array, in modo da evitare la selezione multipla dello stesso target
                        rle.ClearROISelectedArray();
                        GD_AreaSelected.x = rle.x0;
                        GD_AreaSelected.y = rle.y0;
                        GD_AreaSelected.w = rle.width;
                        GD_AreaSelected.h = rle.height;
                        GD_AreaSelected.item = rle.sItem;
                        GD_AreaSelected.value = rle.iValue;
                        
                        return count;
                    }
                }
                else
                {
                    //sono all'esterno della ROI, imposto l'array interno a 0
                    rle.ROISelectedArray[rle.RoiSelArrayCounter] = 0;
                }

                //in ogni caso, incremento il contatore interno dell'array dei posti visionati
                if (++rle.RoiSelArrayCounter >= NumStepForSelection)
                    rle.RoiSelArrayCounter = 0;
                

                //aggiorno il contatore 
                ++count; 
            }

            return -1;
        }

        public int IsInRoi(int _x, int _y){
            int count = 0;
            ROI_to_highlight = NO_ROI;

            foreach (RoiListElement rle in myRoiList)
            {
                if (_x >= rle.x0 && _x <= rle.x1 && _y >= rle.y0 && _y <= rle.y1 && rle.bActive)
                {
                    ROI_to_highlight = count;
                    return count;
                }
                count++;
            }
            return -1;
        }

    }


    public class AreaOfInterestData
    {
        public int x, y, w, h;
        public string item;
        public int value;
    }
}
